#!/usr/bin/env python
#coding=utf-8
from config import LAST_INDEX,RUN_TRAINING_FILE,BIT_TRAINING_FILE

#数据预处理,将待检验数据,根据索引文件转化成0,1
#读索引数据
def GetSourceIndex(filepath):
    file = open (filepath,"r")
    lines = file.readlines()#读进了一个列表
    #print lines
    data = ""
    numIndexArray = []
    for i in range(len(lines)):
        lines[i] = lines[i].strip()
        arr = lines[i].split(' ')
        #fieldName,fieldType = arr[0],arr[-1]
        #numIndexArray.append((i,fieldName,fieldType,i))
        numIndexArray.append([])
        for j in range(len(arr)):
            numIndexArray[i].append(arr[j])
    file.close()
    return numIndexArray

#读检验数据,返回数据矩阵
def ReadDataSource(filepath):
    file = open(filepath,"r")
    lines = file.readlines()
    dataSource = []
    #读第一行,获取列数
    lines[0] = lines[0].rstrip()#除去最后一个回车符
    arrColumn = lines[0].split(',')
    #将所有数据读入list
    for i in range(len(arrColumn)):
        dataSource.append([])#插入一列 
               
    #读取每行
    for i in range(len(lines)):
        lines[i] = lines[i].rstrip()
        arr = lines[i].split(',')
        for j in range(len(arrColumn)):
            dataSource[j].append(arr[j])
        #print i
    return dataSource

# 返回经过属性选择和bool转换的数据矩阵
def TransBit(indexRes,originData):
    #确定indexRes与originData中列的对应关系
    DataBit = []
    for i in range(len(indexRes)):#第一列,即每一Top100属性
        DataBit.append([i+1])#先放一个序号
        intIndex = -1
        for j in range(len(originData)):
            # 根据属性名来找到该列在原始数据中的列号
            if indexRes[i][1] == originData[j][0]:
                #listA.append(j)
                intIndex = j
                break
        if indexRes[i][2] == 'Num':
            rangeNum = int(indexRes[i][3])
            minVal = float(indexRes[i][4])
            maxVal = float(indexRes[i][5])
            # 将原始数据中匹配的列进行转换
            for j in range(1,len(originData[intIndex])):
                floatNum = str(originData[intIndex][j])
                if floatNum.strip() != '' and floatNum != ' ':
                    floatNum = float(floatNum)
                    #maxVal - minVal == 0的情况应考虑:
                    if maxVal - minVal == 0:
                        if floatNum == minVal:
                            DataBit[i].append(1)
                        else:
                            DataBit[i].append(0)
                    else:
                        intCalNum = int((floatNum - minVal)/(maxVal - minVal) * rangeNum)                     
                        bitCalNum = 0
                        for k in range(6,len(indexRes[i])):
                            if intCalNum == int(indexRes[i][k]):
                                bitCalNum = 1
                                break
                        DataBit[i].append(bitCalNum)
                else:
                    DataBit[i].append(0)
        else:
            for j in range(1,len(originData[intIndex])):
                strNum = str(originData[intIndex][j])
                if strNum.strip() != '' and strNum != ' ':
                    bitCalNum = 0
                    for k in range(3,len(indexRes[i])):
                        if strNum == str(indexRes[i][k]):
                            bitCalNum = 1
                            break
                    DataBit[i].append(bitCalNum)
                else:
                    DataBit[i].append(0)
            
    print len(indexRes)
    DataBit.append(['TB'])
    DataBit.append(['TD'])
    tbIndex = 0
    for i in range(len(originData) ):
        if originData[i][0] == 'TARGET_B':
            tbIndex = i
            break
    print "TARGET_B AT:"+str(tbIndex)
    for j in range(1,len(originData[0]) ):
        DataBit[-2].append( originData[tbIndex][j] )
        DataBit[-1].append( originData[tbIndex+1][j] )
    #print indexRes
    #print DataBit
    return DataBit

def writePreData(DataBit,filepath):
    file = open(filepath,"w")
    for i in range(len(DataBit[0])):
        for j in range(len(DataBit)-1):
            file.write(str(DataBit[j][i]) + ',')
        file.write(str(DataBit[len(DataBit)-1][i]) + '\n')
    print "Data has been transformed:"+filepath
    
if __name__ == "__main__":
    filelastpath = BIT_TRAINING_FILE
    indexRes = GetSourceIndex( LAST_INDEX )
    originData = ReadDataSource(RUN_TRAINING_FILE)
    DataBit = TransBit(indexRes,originData)
    writePreData(DataBit,filelastpath)

